package cruds.tires;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

import panels.BodyPanel;
import services.StockableService;
import utils.StaticMethods;
import home.Home;
import items.Stockable;
import cruds.CrudCreateParent;
import dynamicmenus.TiresMenu;
import exceptions.EmptyFieldException;

@SuppressWarnings("serial")
public class CrudCreateTire extends CrudCreateParent<Stockable>{

	private String brand;
	private String model;
	private String priceList;
	private Double purchasePercent;
	private Double salePercent;
	private String measure;
	private Integer stock = 0;
	
	public CrudCreateTire(Home home) {
		super(home);
	}

	@Override
	protected void initializeService() {
		this.setService(this.getHome().getGeneralService().getStockableSer());
	}

	@Override
	protected void redirectToPageIfCancel(AjaxRequestTarget target) {
		BodyPanel page = new BodyPanel(this.getHome());
		page.changePanel(target);
	}

	@Override
	protected void updateDynamicMenu(AjaxRequestTarget target) {
		TiresMenu menu = new TiresMenu(this.getHome());
		menu.changePanel(target);
	}

	@Override
	protected void doSaveAndRedirect(AjaxRequestTarget target) {
		try{
			StaticMethods.accesoryHasEmptyFields("Neumatico",this.brand,this.model, this.measure,
					this.priceList,this.purchasePercent,this.salePercent,this.stock);
			this.getService().save(new Stockable("Neumatico",this.brand,this.model,this.measure
					,new BigDecimal(this.priceList),this.purchasePercent,this.salePercent,this.stock));
			StockableService service = ((StockableService)this.getService());
			TiresList listar = new TiresList(this.getHome(),service.searchTires(this.getBrand()));
			listar.changePanel(target);
		}catch(EmptyFieldException e){
			this.setErrorMessage(target, e.getMessage());
		}
	}

	@Override
	protected void createFormFields(Form<Stockable> form) {
		form.add(new TextField<String>("priceList", new PropertyModel<String>(this,"priceList")));
		form.add(new TextField<Double>("purchasePercent", new PropertyModel<Double>(this,"purchasePercent")));
		form.add(new TextField<Double>("salePercent", new PropertyModel<Double>(this,"salePercent")));
		form.add(new TextField<Integer>("stock", new PropertyModel<Integer>(this,"stock")));
		DropDownChoice<String> measures = new DropDownChoice<String>("measure", new PropertyModel<String>(this, "measure")
				, Arrays.asList("125/30/13","165/60/14","165/40/14","175/70/13"));
		form.add(measures);
		
		final StockableService service = ((StockableService)this.getService());
		final List<String> marcas = service.getBrandService().getTireBrands();
		final WebMarkupContainer cont = new WebMarkupContainer("cont");
		cont.setOutputMarkupId(true);
		form.add(cont);
		
		final DropDownChoice<String> marks = new DropDownChoice<String>("brands", new PropertyModel<String>(this,"brand"), new ArrayList<String>(marcas));
		final DropDownChoice<String> models = new DropDownChoice<String>("models", new PropertyModel<String>(this,"model"), new LoadableDetachableModel<List<String>>(){

			@Override
			protected List<String> load() {
				final String mark = marks.getModelObject();
				final List<String> list = service.getBrandService().getModelsOfMark(mark);
	            return list != null ? list : new ArrayList<String>(0);
			}
		}); 
		
		marks.add(new AjaxFormComponentUpdatingBehavior("onchange"){

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.add(cont);
			}	
		});
		
		cont.add(marks);
		cont.add(models);  
		
	}

	@Override
	protected void initializeEmptyItem() {
		this.setItem(new Stockable());
		
	}

	//---------Sett&Gett
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getPriceList() {
		return priceList;
	}

	public void setPriceList(String priceList) {
		this.priceList = priceList;
	}

	public Double getPurchasePercent() {
		return purchasePercent;
	}

	public void setPurchasePercent(Double purchasePercent) {
		this.purchasePercent = purchasePercent;
	}

	public Double getSalePercent() {
		return salePercent;
	}

	public void setSalePercent(Double salePercent) {
		this.salePercent = salePercent;
	}

	public String getMeasure() {
		return measure;
	}

	public void setMeasure(String measure) {
		this.measure = measure;
	}

	public Integer getStock() {
		return stock;
	}

	public void setStock(Integer stock) {
		this.stock = stock;
	}

}
